package com.sbl.bajw.pc.service;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.PrivilegeDao;
import com.sbl.bajw.pc.model.PrivilegeModel;

@Service
public class PrivilegeService {
	
	@Resource
	private PrivilegeDao dao;
	
	@Resource 
	private MyBatisDao gridDao;
	
	static HashMap<String, String> userPrivilegeMap;
	
	public void insert(PrivilegeModel model) throws MessageException{
		checkModel(model);
		model.setCreater(SessionUnit.getUserId());
		dao.insert(model);
	}
	
	public void update(PrivilegeModel model) throws MessageException{
		checkModel(model);
		dao.update(model);
		initPrivilegeMap();
	}
	
	@Transactional
	public void delete(String id){
		dao.delete(id);
		dao.deletePrivilegeRole(id);
		initPrivilegeMap();
	}
	
	public String list(){
		String result = gridDao.selectPage(PrivilegeDao.class, null, "list");
		return result;
	}
	
	public String listByRole(){
		String id = RequestUnit.getString("roleid");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("roleid", id);
		String result = gridDao.selectPage(PrivilegeDao.class, parm, "listByRole");
		return result;
	}
	
	public List<PrivilegeModel> selectModels(PrivilegeModel model){
		List<PrivilegeModel> models = dao.selectModels(model);
		return models;
	}
	
	public PrivilegeModel getModel(String id){
		PrivilegeModel model = dao.getModel(id);
		return model;
	}
	
	@Transactional
	public void addPrivilegeToRole(){
		String privilegeIdsStr = RequestUnit.getString("peivilegeIds");
		String roleid = RequestUnit.getString("roleid");
		if(privilegeIdsStr == null||roleid == null){
			return;
		}
		dao.deletePrivilegeByRole(roleid);
		String[] privilegeIds = privilegeIdsStr.split(",");
		for(int i=0;i<privilegeIds.length;i++){
			HashMap<String, String> parm = new HashMap<String, String>();
			parm.put("privilege_id", privilegeIds[i]);
			parm.put("role_id", roleid);
			dao.addPrivilegeToRole(parm);
		}
		initPrivilegeMap();
	}
	
	public void checkModel(PrivilegeModel model) throws MessageException{
		List<PrivilegeModel> models = dao.selectModels(model);
		if(models.size() > 0){
			throw new MessageException("权限url，名称，或者key已存在");
		}
		
		if(model.getKey()==null || model.getKey().equals("")){
			throw new MessageException("key不能为空","2");
		}
		
		if(model.getName()==null || model.getName().equals("")){
			throw new MessageException("名称不能为空","2");
		}
		
		if(model.getUrl()==null || model.getUrl().equals("")){
			throw new MessageException("url不能为空","2");
		}
	}
	
	public boolean hasPrivilege(String key){
		String userid = SessionUnit.getUserId();
		if(userPrivilegeMap == null){
			initPrivilegeMap();
		}
		if(userPrivilegeMap.get(userid+key) != null){
			return true;
		}
		else{
			return false;
		}
	}
	
	public  void initPrivilegeMap(){
		List<HashMap<String, String>> list = dao.getUserPrivilegeList();
		if(userPrivilegeMap == null){
			userPrivilegeMap = new HashMap<String, String>();
		}
		else{
			userPrivilegeMap.clear();
		}
		
		if(list == null){
			return;
		}
		
		for(HashMap<String, String> up:list){
			String key = up.get("KEY");
			String userid = up.get("USER_ID");
			userPrivilegeMap.put(userid+key, "1");
		}
	}
}
